package com.myapp.ruyixing.ui.shopmanager;

import static android.view.View.VISIBLE;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.model.ShopPicList;
import com.myapp.ruyixing.model.ShopPicList.ShopPic;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;
import com.myapp.ruyixing.utils.UIUtils;

// 图片审核
public class ShopPhotoCheckActivity extends BaseActivity implements
		OnClickListener {
	private List<ShopPicList.ShopPic> mList;
	private MyListAdapter mAdapter;

	private TitleBar mTitleBar;
	private View mLoadingView;
	private ListView mListView;
	private TextView mEmptyView;

	private int mCurrentIndex = 0;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ShopPicList picList = (ShopPicList) o;
				onLoadingDataFinished(true, picList);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private RYHttpResponseHandler mUpdateHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model result = (Model) o;
				onSetPictureStatusFinished(true, result);
			} catch (Exception e) {
				onSetPictureStatusFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onSetPictureStatusFinished(false, null);
		}
	};

	private void setPictureStatus(int index, String id, int status) {
		mCurrentIndex = index;
		showLoading();
		RYHttpClient.updateShopPic(this, mUpdateHttpResponseHandler,
				LoginHelper.getInstance().getShopId(), LoginHelper
						.getInstance().getShopBduss(), id, status + "");
	}

	private void onSetPictureStatusFinished(boolean succeed, Model result) {
		hideLoading();
		if (succeed && result != null && result.errno == 0) {
			Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
			mList.remove(mCurrentIndex);
			mAdapter.notifyDataSetChanged();
		} else {
			Toast.makeText(this, "更新失败：" + (result == null ? "" : result.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	private void onLoadingDataFinished(boolean succeed, ShopPicList picList) {
		hideLoading();
		if (succeed && picList != null && picList.items != null) {
			mEmptyView.setVisibility(View.GONE);
			mList.addAll(picList.items);
			mAdapter.notifyDataSetChanged();
			if (mList.size() == 0) {
				mEmptyView.setVisibility(VISIBLE);
			}
		} else {
			Toast.makeText(this, "加载失败" + (picList == null ? "" : picList.msg),
					Toast.LENGTH_SHORT).show();
			mEmptyView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_shop_photo_check);

		initView();

		mList = new ArrayList<ShopPicList.ShopPic>();
		mAdapter = new MyListAdapter();
		mListView.setAdapter(mAdapter);

		showLoading();
		RYHttpClient.getShopPicList(this, mHttpResponseHandler, LoginHelper
				.getInstance().getShopBduss(), LoginHelper.getInstance()
				.getShopId(), 0, 50);
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pass:
			// TODO: item审核通过
			UIUtils.showToast(this, "通过");

			break;
		case R.id.reject:
			// TODO: item审核失败
			UIUtils.showToast(this, "未通过");

			break;

		default:
			break;
		}
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	private class ViewHolder {
		private TextView mUserView;
		private TextView mTimeView;
		private TextView mTitleView;
		private NetworkImageView mImageView;
		private View mPassView;
		private View mRejectView;
	}

	private class MyListAdapter extends BaseAdapter {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter() {
			super();
			mQueue = Volley.newRequestQueue(ShopPhotoCheckActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public int getCount() {
			return mList == null ? 0 : mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(ShopPhotoCheckActivity.this)
						.inflate(R.layout.cell_shop_manager_shop_photo_check,
								parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mUserView = (TextView) convertView
						.findViewById(R.id.user);
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);
				holder.mTitleView = (TextView) convertView
						.findViewById(R.id.title);
				holder.mPassView = convertView.findViewById(R.id.pass);
				holder.mRejectView = convertView.findViewById(R.id.reject);
				holder.mImageView = (NetworkImageView) convertView
						.findViewById(R.id.image);

				holder.mPassView
						.setOnClickListener(ShopPhotoCheckActivity.this);
				holder.mRejectView
						.setOnClickListener(ShopPhotoCheckActivity.this);

				convertView.setTag(holder);
			}

			ShopPic shopPic = mList.get(position);

			ViewHolder mHolder = (ViewHolder) convertView.getTag();

			mHolder.mImageView.setImageUrl(shopPic.picurl, mImageLoader);
			mHolder.mTimeView.setText(shopPic.datetime);
			mHolder.mTitleView.setText(shopPic.type);
			mHolder.mUserView.setText(shopPic.name);
			final String id = shopPic.id;
			final int index = position;
			mHolder.mPassView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setPictureStatus(index, id, 1);
				}
			});
			mHolder.mRejectView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setPictureStatus(index, id, 2);
				}
			});

			return convertView;

		}
	}
}
